<template>
  <el-dialog
    title="选择财物"
    append-to-body
    :visible.sync="dialogVisible"
    width="90%"
  >
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item prop="taskNo">
            <el-input v-model="queryParams.taskNo" placeholder="处置编号" clearable size="small"
                      @keyup.enter.native="getList"
            />
          </el-form-item>
          <el-form-item prop="proNo">
            <el-input v-model="queryParams.proNo" placeholder="物品编号" clearable size="small"
                      @keyup.enter.native="getList"
            />
          </el-form-item>
          <el-form-item prop="region">
            <el-select v-model="queryParams.region" placeholder="区域" clearable size="small">
              <el-option
                v-for="dict in dict.type.wms_put_task_area"
                v-if="dict.value !=='QB'"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="queryParams.inCenter" placeholder="在管理中心" clearable size="small">
              <el-option label="是" value="1"/>
              <el-option label="否" value="0"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <div class="mb5">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPro">添加</el-button>
    </div>
    <el-table class="mt10" v-loading="loading" :data="dealList"
              @selection-change="handleSelectionChange" border stripe
    >
      <el-table-column type="selection" :selectable="checkSelectable" width="55" align="center"/>
      <el-table-column label="处置编号" prop="taskNo" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.taskNo }}
        </template>
      </el-table-column>
      <el-table-column label="物品编号" prop="proNo" align="center" width="240"/>
      <el-table-column label="物品名称" prop="proName" align="center" min-width="200"/>
      <el-table-column label="申请单位" prop="handleUnitName" align="center" min-width="260">
        <template slot-scope="scope">
          {{ scope.row.applyUnit }}
        </template>
      </el-table-column>
      <el-table-column label="处置数量" prop="applyNumber" align="center" min-width="100"/>
      <el-table-column v-if="queryParams.disposalType ==='5'" label="处置价值" align="center" min-width="100">
        <template slot-scope="scope">
          <el-input-number :controls="false" style="width: 80%" v-model="scope.row.disposalAmount"/>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" prop="applyTime" align="center" min-width="260"/>
      <el-table-column label="在管理中心" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.inCenter ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="需要评估" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.needEvaluate ? '是' : '否' }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[10,50,100,200,500]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>
<script>
import { listDisposalNotRegister } from '@/api/oms/dealTask'
import { addPro } from '@/api/sacw/dispose'

export default {
  dicts: ['wms_put_task_area'],
  data() {
    return {
      dialogVisible: false,
      total: 0,
      loading: false,
      disposalId: undefined,
      selectList: [],
      dealList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        disposalType: ''
      }
    }
  },
  methods: {
    open(disposalType, disposalId) {
      this.dialogVisible = true
      this.disposalId = disposalId
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        disposalType: disposalType
      }
      setTimeout(() => {
        this.getList()
      }, 100)
    },
    getList() {
      this.loading = true
      listDisposalNotRegister(this.queryParams).then(response => {
        this.dealList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    checkSelectable(row) {
      return !row.needEvaluate
    },
    handleSelectionChange(selection) {
      this.selectList = selection
    },
    addPro() {
      if (this.selectList && this.selectList.length) {
        if (this.queryParams.disposalType === '5') {
          const filter = this.selectList.filter(({ disposalAmount }) => disposalAmount === undefined)
          if (filter && filter.length) {
            return this.$message.warning('请填写财物的处置金额')
          }
        }
        this.$modal.confirm(`是否确认添加${this.selectList.length}个物品？`).then(() => {
          const data = this.selectList.map(item => {
            return {
              taskId: item.taskId,
              disposalId: this.disposalId,
              proNo: item.proNo,
              disposalNumber: item.applyNumber,
              disposalAmount: item.disposalAmount
            }
          })
          this.$modal.loading('添加中....')
          addPro(data).then(() => {
            this.$modal.msgSuccess('添加成功')
            this.dialogVisible = false
            this.$emit('refresh')
          }).finally(() => {
            this.$modal.closeLoading()
          })
        })
      } else {
        this.$modal.msgError('请选择物品')
      }
    }
  }
}
</script>
